﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WR.BL.BE;
using SAP.Middleware.Connector;

namespace WR.DL.DALC.SAP
{
    public class ClientDALC
    {
        private string _connId = string.Empty;

        #region Singleton
        public static ClientDALC getInstance(string pConnId)
        {
            if (_instance == null)
            {
                lock (typeof(ClientDALC))
                {
                    if (_instance == null)
                    {
                        _instance = new ClientDALC(pConnId);
                    }
                }
            }
            return _instance;
        }
        private ClientDALC(string pConnId) { this._connId = pConnId; }
        private static volatile ClientDALC _instance = null;
        #endregion

        public string cliEventual(string pCod)
        {
            string retval = string.Empty;
            IRfcFunction function = null;
            InMemoryDestinationConfiguration dc = new InMemoryDestinationConfiguration();
            try
            {
                //RfcConfigParameters cp = new RfcConfigParameters(); //GenericDALC.getConfigParam(pObjConn);
                //cp.Add(RfcConfigParameters.Name, this._connId);
                //RfcDestination dest = RfcDestinationManager.GetDestination(cp);
                try
                {
                    RfcDestinationManager.RegisterDestinationConfiguration(dc);
                }
                catch { }
                RfcDestination dest = RfcDestinationManager.GetDestination(this._connId);
                function = dest.Repository.CreateFunction("ZWR_SD_015");
                function.SetValue("I_KUNNR", pCod);
                function.Invoke(dest);

                retval = function.GetValue("E_RETURN").ToString();
            }
            catch (RfcBaseException e)
            {
                // Grabar Log de errores con e.message
                //throw e;
            }
            finally
            {
                RfcDestinationManager.UnregisterDestinationConfiguration(dc);
            }
            return retval;
        
        }

        public ClientBE read(string pCod)
        {
            ClientBE objCli = null; IRfcFunction function = null;
            InMemoryDestinationConfiguration dc = new InMemoryDestinationConfiguration();
            try
            {
                //RfcConfigParameters cp = new RfcConfigParameters(); //GenericDALC.getConfigParam(pObjConn);
                //cp.Add(RfcConfigParameters.Name, this._connId);
                //RfcDestination dest = RfcDestinationManager.GetDestination(cp);
                try
                {
                    RfcDestinationManager.RegisterDestinationConfiguration(dc);
                }
                catch { }
                RfcDestination dest = RfcDestinationManager.GetDestination(this._connId);
                function = dest.Repository.CreateFunction("ZWR_SD_002");
                function.SetValue("I_KUNNR", pCod);
                function.Invoke(dest);

                IRfcTable T_KNA1 = function.GetTable("T_KNA1");
                foreach (IRfcStructure s_kna1 in T_KNA1)
                {
                    objCli = new ClientBE();
                    objCli.Codigo = s_kna1["KUNNR"].GetString();
                    objCli.Descripcion = s_kna1["KUNNR"].GetString();
                    objCli.Ruc = s_kna1["STCD1"].GetString();
                    objCli.Ramo = s_kna1["BRSCH"].GetString();
                    objCli.NomRamo = s_kna1["BRTXT"].GetString();
                    objCli.Correo = s_kna1["SMTP_ADDR"].GetString();
                    objCli.Lprio = s_kna1["LPRIO"].GetString();
                    objCli.Moneda = s_kna1["WAERS"].GetString();
                    objCli.CondPago = s_kna1["ZTERM"].GetString();
                    objCli.OfVenta = s_kna1["VKBUR"].GetString();
                }
            }
            catch (RfcCommunicationException e)
            {
                // network problem...
            }
            catch (RfcLogonException e)
            {
                // user could not logon...
            }
            catch (RfcAbapRuntimeException e)
            {
                // serious problem on ABAP system side...
            }
            catch (RfcAbapBaseException e)
            {
                // The function module returned an ABAP exception, an ABAP message
                // or an ABAP class-based exception...
            }
            catch (RfcBaseException e)
            {
                // Grabar Log de errores con e.message
                //throw new Exception(e.Message);
            }
            finally
            {
                RfcDestinationManager.UnregisterDestinationConfiguration(dc);
            }
            return objCli;
        }

        public List<ClientBE> find(string pCod, string pDesc, string pRamo)
        {
            //Máximo de registros
            int count = 20;
            try { count = Convert.ToInt16(DALC.getParamValue("MAXCLI")); }
            catch { }
            //-------------------

            List<ClientBE> lstCli = new List<ClientBE>(); ClientBE objCli = null; IRfcFunction function = null;
            InMemoryDestinationConfiguration dc = new InMemoryDestinationConfiguration();
            try
            {
                //RfcConfigParameters cp = new RfcConfigParameters(); //GenericDALC.getConfigParam(pObjConn);
                //cp.Add(RfcConfigParameters.Name, this._connId);
                //RfcDestination dest = RfcDestinationManager.GetDestination(cp);
                try
                {
                    RfcDestinationManager.RegisterDestinationConfiguration(dc);
                }
                catch { }
                RfcDestination dest = RfcDestinationManager.GetDestination(this._connId);
                function = dest.Repository.CreateFunction("ZWR_SD_002");
                if (!string.IsNullOrEmpty(pCod))
                {
                    function.SetValue("I_KUNNR", pCod);
                }
                if (!string.IsNullOrEmpty(pDesc))
                {
                    function.SetValue("I_NAME1", pDesc);
                }
                if (!string.IsNullOrEmpty(pRamo))
                {
                    function.SetValue("I_BRSCH", pRamo);
                }
                function.Invoke(dest);

                IRfcTable T_KNA1 = function.GetTable("T_KNA1");
                foreach (IRfcStructure s_kna1 in T_KNA1)
                {
                    objCli = new ClientBE();
                    objCli.Codigo = s_kna1["KUNNR"].GetString();
                    objCli.Descripcion = s_kna1["NAME1"].GetString();
                    objCli.Ruc = s_kna1["STCD1"].GetString();
                    objCli.Ramo = s_kna1["BRSCH"].GetString();
                    objCli.NomRamo = s_kna1["BRTXT"].GetString();
                    objCli.Correo = s_kna1["SMTP_ADDR"].GetString();
                    objCli.Lprio = s_kna1["LPRIO"].GetString();
                    objCli.Moneda = s_kna1["WAERS"].GetString();
                    objCli.CondPago = s_kna1["ZTERM"].GetString();
                    objCli.OfVenta = s_kna1["VKBUR"].GetString();

                    lstCli.Add(objCli);
                }
            }
            catch (RfcCommunicationException e)
            {
                // network problem...
            }
            catch (RfcLogonException e)
            {
                // user could not logon...
            }
            catch (RfcAbapRuntimeException e)
            {
                // serious problem on ABAP system side...
            }
            catch (RfcAbapBaseException e)
            {
                // The function module returned an ABAP exception, an ABAP message
                // or an ABAP class-based exception...
            }
            catch (RfcBaseException e)
            {
                // Grabar Log de errores con e.message
                //throw e;
            }
            finally
            {
                RfcDestinationManager.UnregisterDestinationConfiguration(dc);
            }
            return lstCli;
        }
    }
}
